﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Web.UI;

namespace Detai.Lib.Helpers
{
	public static class StateHelpers
	{
		public static string State(this HtmlHelper helper, string name, int selectedValue)
		{
			return State(helper, name, selectedValue, null);
		}
		public static string State(this HtmlHelper helper, string name, int selectedValue, string scriptAction)
		{
			StringBuilder htmlBuilder = new StringBuilder();
			using (TextWriter stringWriter = new StringWriter(htmlBuilder))
			{
				using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
				{
					// Select tag
					if (scriptAction != null)
					{
						writer.AddAttribute(HtmlTextWriterAttribute.Onchange, scriptAction);
					}
					writer.AddAttribute(HtmlTextWriterAttribute.Name, name);
					writer.AddAttribute(HtmlTextWriterAttribute.Id, name);
					writer.RenderBeginTag(HtmlTextWriterTag.Select);

					// Unsellect tag
					writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
					writer.RenderBeginTag(HtmlTextWriterTag.Option);
					writer.Write("-- Trạng thái --");
					writer.RenderEndTag();

					// Waiting for replying
					writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
					if (selectedValue == 0)
					{
						writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");
					}
					writer.RenderBeginTag(HtmlTextWriterTag.Option);
					writer.Write("Chưa trả lời");
					writer.RenderEndTag();

					// Replied
					writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
					if (selectedValue == 1)
					{
						writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");
					}
					writer.RenderBeginTag(HtmlTextWriterTag.Option);
					writer.Write("Đã trả lời");
					writer.RenderEndTag();

					writer.RenderEndTag();
				}
			}
			return htmlBuilder.ToString();
		}
	}
}
